import apiUrls from "@/apis/config.js"

//上传图片
export const pushImgs = (callback) => {
	uni.chooseImage({
		count: 1,
		success: (res) => {
			const tempFilePaths = res.tempFilePaths;

			uni.showLoading({
				title: "正在上传..."
			})
			let header = ''
			if (tempFilePaths[0].indexOf('wxfile') == -1) {
				header = {
					'Content-Type': 'application/x-www-form-urlencoded'
				}
			}
			uni.uploadFile({
				url: apiUrls.baseURL + '/common/upload/qiniu/file', //post请求的地址
				filePath: tempFilePaths[0],
				name: 'file',
				header: header,
				formData: {
					img: tempFilePaths[0] //formData是指除了图片以外，额外加的字段
				},
				success: (uploadFileRes) => {
					//这里要注意，uploadFileRes.data是个String类型，要转对象的话需要JSON.parse一下
					let uploadData = JSON.parse(uploadFileRes.data);
					if (uploadData.errno == 0) {
						console.log('上传成功')
						uni.hideLoading()
						callback(uploadData.obj)
					} else {
						uni.hideLoading()
						console.error(uploadData.error)
					}
				},
				fail: (err) => {
					uni.hideLoading()
					console.error(err)
				}
			})
		}
	})
}
